<?php

function pagination($url, $page, $total, $addText = '&') {
    $html = '<ul class="pagination">';
    
    $adj = 2;
    
    //đầu First
    $html .= "<li><a href=\"{$url}\">Đầu</a></li>";
    
    //trước prev <<
    if ($page>1) {
        $prev = $page-1;
        $html .= "<li><a href=\"{$url}{$addText}page=$prev\">Trước</a></li>";
    } 
    
    //đoạn giữa
    $pmin = ($page>$adj) ? $page-$adj : 1;
    $pmax = ($page+$adj<$total) ? $page+$adj : $total;
    
    for($i=$pmin; $i<=$pmax; $i++) {
        if ($page==$i) {
            $html .= "<li class=\"active\"><a href=\"{$url}{$addText}page=$i\">$i</a></li>";
        } else {
            $html .= "<li><a href=\"{$url}{$addText}page=$i\">$i</a></li>";
        }
    }
    
    //tiếp theo Next
    if ($page<$total) {
        $next = $page+1;
        $html .= "<li><a href=\"{$url}{$addText}page=$next\">Tiếp</a></li>";
    } 
    
    //Cuối : Last
    $html .= "<li><a href=\"{$url}{$addText}page=$total\">Cuối</a></li>";
    
    $html .= '</ul>';
    
    return $html;
}